package com.rain.example.verifyPermissions.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rain.example.verifyPermissions.constants.Constants;
import com.rain.example.verifyPermissions.models.business.Business;
import com.rain.example.verifyPermissions.response.Result;
import com.rain.example.verifyPermissions.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author rain
 * @since 2024-07-31
 */
@RestController
@RequestMapping("/business")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @PostMapping("/save")
    @Transactional
    public Result save(@RequestBody Business params) {
        params.setDeleteFlag(Constants.INT_ZERO);
        boolean save = businessService.save(params);
        if (!save) {
            throw new RuntimeException("保存失败");
        }
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(String name) {
        List<Business> list = businessService.list(Wrappers.<Business>lambdaQuery().like(Business::getName, name).eq(Business::getDeleteFlag, Constants.INT_ZERO));
        return Result.success(list);
    }
}
